PreloadImages = function (images/*Array*/, callback, loadingIndicator/*optional*/) {
    var aImages = images;
    this.loadingIndicator = typeof(loadingIndicator) != "undefined"
                            ? loadingIndicator
                            : null;
    //store the callback
    this.callback = callback;
    this.images = new Array();
    this.imagesLength = aImages.length;
    this.processed = 0;
    this.loaded = 0;

    for (var i = 0 ; i < aImages.length ; i++) {
        this.preLoadImages(aImages[i]);
    }
}
    PreloadImages.prototype.preLoadImages = function (image) {
        //create new Image object and add to array
        var oImage = new Image();
        this.images.push(oImage);

        //set up event handlers for the Image object
        oImage.onload = PreloadImages.prototype.onload;
        oImage.onerror = PreloadImages.prototype.onerror;
        oImage.onabort = PreloadImages.prototype.onabort;

        //assign pointer back to this.
        oImage.oImagePreloader = this;
        oImage.bLoaded = false;

        //assign the .src property of the Image object
        oImage.src = image;
    }
    PreloadImages.prototype.onComplete = function() {
        this.processed++;
        //document.getElementById("loading").innerHTML = Math.round((this.loaded/this.imagesLength)*100) + " %";
        if ( this.loadingIndicator != null ) {
            this.loadingIndicator.innerHTML = Math.round((this.loaded/this.imagesLength)*100) + " %";
        }
        /*if ( this.processed == this.imagesLength ) { //callback on processed all images
            this.callback(this.images, this.loaded);
        }*/
        if ( this.loaded == this.imagesLength ) { //callback on loaded all images
            this.callback();
        }
    }

    PreloadImages.prototype.onload = function() {
        this.bLoaded = true;
        this.oImagePreloader.loaded++;
        this.oImagePreloader.onComplete();
    }

    PreloadImages.prototype.onerror = function() {
        this.bError = true;
        this.oImagePreloader.onComplete();
    }

    PreloadImages.prototype.onabort = function() {
        this.bAbort = true;
        this.oImagePreloader.onComplete();
    }
